#ifndef _THREND_HPP_
#define _THREND_HPP_
#include <iostream>
#include <string>
#include <pthread.h>
#include <functional>
#include <sys/types.h>
#include <unistd.h>

/*namespace ThreadModule
{
    using func_t = std::function<void()>;
    static int number = 1;
    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    class Thread
    {
    private:
        // 成员方法！
        static void *Routine(void *args)
        {
            Thread *t = static_cast<Thread *>(args);
            t->_status = TSTATUS::RUNNING;
            t->_func();
            return nullptr;
        }
        void EnableDetach() { _joinable = false; }

    public:
        Thread(func_t func) : _func(func), _status(TSTATUS::NEW), _joinable(true)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        bool Start()
        {
            if (_status != TSTATUS::RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this); // TODO
                if (n != 0)
                    return false;
                return true;
            }
            return false;
        }
        bool Stop()
        {
            if (_status == TSTATUS::RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                std::cout<<"我调用了pthread_cancel"<<std::endl;
                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if (_joinable)
            {
                void* ret = nullptr;
                int n = ::pthread_join(_tid, &ret);
                std::cout<<"我调用了pthread_join"<<std::endl;

                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        void Detach()
        {
            EnableDetach();
            pthread_detach(_tid);
        }
        bool IsJoinable() { return _joinable; }
        std::string Name() { return _name; }
        ~Thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable; // 是否是分离的，默认不是
        func_t _func;
        TSTATUS _status;
    };
}*/
/*namespace ThreadModule
{
    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    static int number = 1;
    template<typename T>
    class Thread
    {
        public:
        using func_t = std::function<void(T)>;
    private:


        // 成员方法！
        static void *Routine(void *args)
        {
            Thread *t = static_cast<Thread<T> *>(args);
            t->_status = TSTATUS::RUNNING;
            t->_func(t->_data);
            return nullptr;
        }
        void EnableDetach() { _joinable = false; }

    public:
        Thread(func_t func,T data) : _func(func), _status(TSTATUS::NEW), _joinable(true),_data(data)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        bool Start()
        {
            if (_status != TSTATUS::RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this); // TODO
                if (n != 0)
                    return false;
                return true;
            }
            return false;
        }
        bool Stop()
        {
            if (_status == TSTATUS::RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                std::cout<<"我调用了pthread_cancel"<<std::endl;
                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if (_joinable)
            {
                void* ret = nullptr;
                int n = ::pthread_join(_tid, &ret);
                std::cout<<"我调用了pthread_join"<<std::endl;

                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        void Detach()
        {
            EnableDetach();
            pthread_detach(_tid);
        }
        bool IsJoinable() { return _joinable; }
        std::string Name() { return _name; }
        ~Thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable; // 是否是分离的，默认不是
        func_t _func;
        TSTATUS _status;
        T _data;
    };
}*/

namespace ThreadModule
{
    static int num = 1;
    using func_t = std::function<void(std::string name)>;//传进来的是一个带参数的可调用对象

    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    class Thread // 线程管理者
    {

    public:
        static void *Routine(void *args)
        {
            Thread *ptr = static_cast<Thread *>(args);
            ptr->_func(ptr->_name);
            ptr->_status = TSTATUS::RUNNING;
            return nullptr;
        }

        Thread(func_t func) : _func(func), Joinable(true)
        {
            _pid = getpid();
            _name = "thread-" + std::to_string(num++);
            _status = TSTATUS::NEW;
        }

        void EnableDetach() // 使之分离
        {
            Joinable = false;
        }

        bool Start() // 运行线程
        {
            if (_status == TSTATUS::NEW)
            {
                int n = pthread_create(&_tid, nullptr, Routine, this);
                std::cout<<"我调用了pthread_create"<<std::endl;
                if(n!=0)
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        std::string Name()
        {
            return _name;
        }

        bool Join()
        {
            //if(_status==TSTATUS::RUNNING && Joinable)有问题，状态为stop的时候也需要Join
            if(Joinable)
            {
                int n =  pthread_join(_tid,nullptr);
                std::cout<<"我调用了pthread_join"<<std::endl;
                if(n!=0)
                {
                    return false;
                }
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        bool Stop()
        {
            if(_status==TSTATUS::RUNNING)
            {
                int n =  pthread_cancel(_tid);
                _status = TSTATUS::STOP;
                if(n!=0)
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        void Detach()
        {
            EnableDetach();
            pthread_detach(_tid);
        }

        ~Thread()
        {
        }

    private:
        pid_t _pid;
        pthread_t _tid;
        std::string _name;
        func_t _func;
        bool Joinable;
        TSTATUS _status;
    };

}

#endif

// #endif
